using System;
using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
using UnityEngine;

public class ChainPopBuilder : MonoBehaviour
{
	private void Awake()
	{
		this.Init();
	}

	private void OnEnable()
	{
		base.StartCoroutine(this.AnimateChainPop());
	}

	private void Init()
	{
		this.GetTk2dUIItem(base.transform);
		this.RemoveItemsByIgnoreList();
		this.ArrangeItems(this.IsAscending());
	}

	private void GetTk2dUIItem(Transform item)
	{
		tk2dUIItem component = item.GetComponent<tk2dUIItem>();
		if (component != null && !this.items.Contains(item))
		{
			this.items.Add(item);
		}
		for (int i = 0; i < item.transform.childCount; i++)
		{
			this.GetTk2dUIItem(item.GetChild(i));
		}
	}

	private void ArrangeItems(bool isAscending)
	{
		if (isAscending)
		{
			this.items.Sort((Transform a, Transform b) => a.position.y.CompareTo(b.position.y));
		}
		else
		{
			this.items.Sort((Transform a, Transform b) => b.position.y.CompareTo(a.position.y));
		}
	}

	private bool IsAscending()
	{
		return this.direction != ChainPopBuilder.Y_DIRECTION.DOWN;
	}

	private void RemoveItemsByIgnoreList()
	{
		if (this.ignoreList.Length <= 0)
		{
			return;
		}
		for (int i = 0; i < this.items.Count; i++)
		{
			for (int j = 0; j < this.ignoreList.Length; j++)
			{
				if (this.items[i].name == this.ignoreList[j].name)
				{
					this.items.Remove(this.items[i]);
				}
			}
		}
	}

	private IEnumerator AnimateChainPop()
	{
		HOTween.Complete(base.transform.name);
		if (this.items.Count <= 0)
		{
			yield return new WaitForSeconds(0.01f);
			this.Init();
		}
		int count = 0;
		Sequence seq = new Sequence(new SequenceParms().Id(base.transform.name));
		foreach (Transform item in this.items)
		{
			if (this.IsSamePositionWithPrevItem(item))
			{
				count--;
			}
			float p_time = (float)count * this.popIntervalSec + this.delaySecOnStart;
			seq.Insert(p_time, this.MakeTweener(item));
			count++;
		}
		seq.Play();
		yield break;
	}

	private bool IsSamePositionWithPrevItem(Transform item)
	{
		int num = this.items.IndexOf(item);
		return num != 0 && item.position.y == this.items[num - 1].position.y;
	}

	private Tweener MakeTweener(Transform item)
	{
		TweenParms p_parms = new TweenParms().Prop("localScale", Vector3.zero).Ease(EaseType.EaseInQuad).OnStart(delegate()
		{
			if (!item.gameObject.activeSelf)
			{
				item.gameObject.SetActive(true);
			}
		}).OnComplete(delegate()
		{
			this.AnimateLikePudding(item);
		});
		return HOTween.From(item, 0.2f, p_parms);
	}

	private void AnimateLikePudding(Transform item)
	{
		TweenParms p_parms = new TweenParms().Prop("localScale", Vector3.one * 0.95f).Ease(EaseType.EaseInOutBack);
		HOTween.From(item, 0.2f, p_parms);
	}

	public ChainPopBuilder.Y_DIRECTION direction = ChainPopBuilder.Y_DIRECTION.DOWN;

	public float delaySecOnStart;

	public float popIntervalSec = 0.03f;

	public Transform[] ignoreList;

	private List<Transform> items = new List<Transform>();

	public enum Y_DIRECTION
	{
		UP,
		DOWN
	}
}
